package com.favccxx.search.api;

import com.favccxx.search.application.DataSourceService;
import com.favccxx.search.application.ProjectService;
import com.favccxx.search.domain.datasource.datasource.AbstractDataSource;
import com.favccxx.search.domain.datasource.datasource.JdbcConnection;
import com.favccxx.search.domain.datasource.datasource.JdbcDataSource;
import com.favccxx.search.domain.datasource.datasource.viewobject.JdbcDataSourceVo;
import com.favccxx.search.domain.project.Project;
import com.favccxx.search.infrastructure.exception.AppException;
import com.favccxx.search.infrastructure.exception.ExceptionCode;
import com.favccxx.search.infrastructure.util.rest.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author favccxx
 */
@Slf4j
@Api(tags = "2.0 Data Source Interface")
@RequestMapping("/datasource")
@RestController
public class DataSourceController {

    @Autowired
    DataSourceService dataSourceService;
    @Autowired
    ProjectService projectService;

    @ApiOperation("2.1 Get data source list")
    @GetMapping("/list")
    public RestResult<List<AbstractDataSource>> findAll() {
        List<AbstractDataSource> list = dataSourceService.getDataSources();
        return RestResult.sucess(list);
    }

    @ApiOperation("2.3 Insert jdbc data source")
    @PutMapping("/jdbc/insert")
    public RestResult<JdbcDataSourceVo> insertJdbc(
            @RequestParam(value = "projectId", required = true) String projectId,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "driver", required = true) String driver,
            @RequestParam(value = "url", required = true) String url,
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "schema", required = false) String schema) {

        Project project = projectService.findById(projectId);
        if (project == null) {
            throw new AppException(ExceptionCode.DataSource.INSERT_DATA_SOURCE_FAILED, "invalid projectId: ->" + projectId);
        }


        JdbcDataSource jdbcDataSource = new JdbcDataSource();
        jdbcDataSource.setCode(code);
        jdbcDataSource.setName(name);
        jdbcDataSource.setProjectId(projectId);

        JdbcConnection jdbcConnection = new JdbcConnection();
        jdbcConnection.setDriver(driver);
        jdbcConnection.setUrl(url);
        jdbcConnection.setUsername(username);
        jdbcConnection.setPassword(password);
        jdbcConnection.setVersion(version);
        jdbcConnection.setSchema(schema);
        jdbcDataSource.setJdbcConnection(jdbcConnection);
        dataSourceService.saveJdbcDataSource(jdbcDataSource);

        JdbcDataSourceVo jdbcDataSourceVo = new JdbcDataSourceVo();
        BeanUtils.copyProperties(jdbcDataSource, jdbcDataSourceVo);
        BeanUtils.copyProperties(jdbcConnection, jdbcDataSourceVo);

        return RestResult.sucess(jdbcDataSourceVo);
    }

    @ApiOperation("2.4 Update jdbc data source")
    @PutMapping("/jdbc/update")
    public RestResult<JdbcDataSourceVo> updateJdbc(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "driver", required = true) String driver,
            @RequestParam(value = "url", required = true) String url,
            @RequestParam(value = "username", required = true) String username,
            @RequestParam(value = "password", required = true) String password,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "schema", required = false) String schema) {
        JdbcDataSource jdbcDataSource = new JdbcDataSource();
        jdbcDataSource.setId(id);
        jdbcDataSource.setCode(code);
        jdbcDataSource.setName(name);

        JdbcConnection jdbcConnection = new JdbcConnection();
        jdbcConnection.setDriver(driver);
        jdbcConnection.setUrl(url);
        jdbcConnection.setUsername(username);
        jdbcConnection.setPassword(password);
        jdbcConnection.setVersion(version);
        jdbcConnection.setSchema(schema);
        jdbcDataSource.setJdbcConnection(jdbcConnection);
        dataSourceService.saveJdbcDataSource(jdbcDataSource);

        JdbcDataSourceVo jdbcDataSourceVo = new JdbcDataSourceVo();
        BeanUtils.copyProperties(jdbcDataSource, jdbcDataSourceVo);
        BeanUtils.copyProperties(jdbcConnection, jdbcDataSourceVo);

        return RestResult.sucess(jdbcDataSourceVo);
    }


}
